//
//  VouchersModel.swift
//  Pods
//
//  Created by eme on 2017/3/29.
//
//

import Foundation
import SwiftyJSON
import ModelProtocol

class VouchersModel: ModelProtocol {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kVouchersVusetimeKey: String = "vusetime"
    internal let kVouchersVcostKey: String = "vcost"
    internal let kVouchersMidKey: String = "mid"
    internal let kVouchersVidKey: String = "vid"
    internal let kVouchersVremarkKey: String = "vremark"
    internal let kVouchersVpdtypeKey: String = "vpdtype"
    internal let kVouchersVstateKey: String = "vstate"
    internal let kVouchersUseridKey: String = "userid"
    internal let kVouchersVstartKey: String = "vstart"
    internal let kVouchersUvidKey: String = "uvid"
    internal let kVouchersVmoneyKey: String = "vmoney"
    internal let kVouchersVnameKey: String = "vname"
    internal let kVouchersVgettimeKey: String = "vgettime"
    internal let kVouchersVendKey: String = "vend"
    
    
    // MARK: 属性
    var vusetime: String
    var vcost: Double
    var mid: String
    var vid: String
    var vremark: String
    var vpdtype: String
    var vstate: String
    var userid: String
    var vstart: String
    var uvid: String
    var vmoney: Double
    var vname: String!
    var vgettime: String!
    var vend: String!
    var mname:String!
    var pdids:String!
    var sinmul:String!
    var showTitle:String{
        get{
            var cost = ""
            /// 如果小数位包含了则需要包含两位小数 否则不保留
            if Double(Int(self.vcost)) == self.vcost {
                cost = "\(Int(self.vcost))"
            }else{
                cost = self.vcost.getFormatStr()
            }
            var money = ""
            if Double(Int(self.vmoney)) == self.vmoney {
                money = "\(Int(self.vmoney))"
            }else{
                money = self.vmoney.getFormatStr()
                
            }
            return "省\(cost)元:满\(money)元减\(cost)元优惠券"
        }
    }
    //适用商品条件
    var conditions:String{
        get{
            var cost = ""
            /// 如果小数位包含了则需要包含两位小数 否则不保留
            if Double(Int(self.vcost)) == self.vcost {
                cost = "\(Int(self.vcost))"
            }else{
                cost = "\(self.vcost)"
            }
            var money = ""
            if Double(Int(self.vmoney)) == self.vmoney {
                money = "\(Int(self.vmoney))"
            }else{
                money = "\(self.vmoney)"
                
            }
            return "满\(money)减\(cost)"
        }
        
    }
    //使用条件()
    var money:String{
        get{
            if self.vmoney == 0{
                return "无门槛"
            }else{
                if Double(Int(self.vmoney)) == self.vmoney{
                    return "满¥\(Int(self.vmoney))使用"
                }else{
                    return "满\(self.vmoney.getPriceStr())使用"
                }
            }
        }
    }
    var cost:String{
        get{
            if Double(Int(self.vcost)) == self.vcost{
                return "¥ \(Int(self.vcost))"
            }else{
                return self.vcost.getPriceStr()
            }
        }
    }
    
    
    
    // MARK: 实现MikerSwiftJSONAble 协议， 解析json数据
    required  init?(json: JSON) {
        vusetime = json[kVouchersVusetimeKey].stringValue
        vcost = json[kVouchersVcostKey].doubleValue
        mid = json[kVouchersMidKey].stringValue
        vid = json[kVouchersVidKey].stringValue
        vremark = json[kVouchersVremarkKey].stringValue
        vpdtype = json[kVouchersVpdtypeKey].stringValue
        vstate = json[kVouchersVstateKey].stringValue
        userid = json[kVouchersUseridKey].stringValue
        vstart = json[kVouchersVstartKey].stringValue
        uvid = json[kVouchersUvidKey].stringValue
        vmoney = json[kVouchersVmoneyKey].doubleValue
        vname = json[kVouchersVnameKey].stringValue
        vgettime = json[kVouchersVgettimeKey].stringValue
        vend = json[kVouchersVendKey].stringValue
        mname = json["mname"].stringValue
        pdids = json["pdids"].stringValue
        sinmul = json["sinmul"].stringValue
        
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    internal func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
        
        dictionary.updateValue(vusetime as AnyObject, forKey: kVouchersVusetimeKey)
    
        dictionary.updateValue(vcost as AnyObject, forKey: kVouchersVcostKey)
    
        dictionary.updateValue(mid as AnyObject, forKey: kVouchersMidKey)
    
        dictionary.updateValue(vid as AnyObject, forKey: kVouchersVidKey)
    
        dictionary.updateValue(vremark as AnyObject, forKey: kVouchersVremarkKey)
    
        dictionary.updateValue(vpdtype as AnyObject, forKey: kVouchersVpdtypeKey)
    
        dictionary.updateValue(vstate as AnyObject, forKey: kVouchersVstateKey)
    
        dictionary.updateValue(userid as AnyObject, forKey: kVouchersUseridKey)
    
        dictionary.updateValue(vstart as AnyObject, forKey: kVouchersVstartKey)
    
        dictionary.updateValue(uvid as AnyObject, forKey: kVouchersUvidKey)
    
        dictionary.updateValue(vmoney as AnyObject, forKey: kVouchersVmoneyKey)
    
        dictionary.updateValue(vname as AnyObject, forKey: kVouchersVnameKey)
    
        dictionary.updateValue(vgettime as AnyObject, forKey: kVouchersVgettimeKey)
    
        dictionary.updateValue(vend as AnyObject, forKey: kVouchersVendKey)
        
        
        return dictionary
    }
    
    
}
